package com.dianning.web.action.user;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.*;
import org.apache.struts2.interceptor.SessionAware;

import com.dianning.bean.Company;
import com.dianning.bean.Message;
import com.dianning.bean.Resume;
import com.dianning.bean.UserDetail;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.comm.OrderObj;
import com.dianning.comm.Util;
import com.dianning.service.InfoService;
import com.dianning.service.MessageService;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;
import com.dianning.service.web.PageList;
import com.dianning.service.web.Pages;
import com.dianning.web.action.BaseAction;
import com.dianning.web.interceptor.UserSessionAware;
import com.dianning.web.servlet.UserSession;

public class MessageAction extends BaseAction implements SessionAware, UserSessionAware{

	private static final Log logger = LogFactory.getLog(MessageAction.class);

	private UserInfoService userInfoService;
	private MessageService messageService;


	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	private UserSession userSession;
	private Map session;
	
	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	private long id;
	private String username;
	private String message;
	private String verifycode;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getVerifycode() {
		return verifycode;
	}

	public void setVerifycode(String verifycode) {
		this.verifycode = verifycode;
	}
	
	private PageList pageList;

	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	

	
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}
	
	public String inbox(){
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.USER_PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/user/message.action?action=inbox");
		this.setPageList(this.getMessageService().findMessages(-1, userSession.getUid(), "inbox", -1, 0, "addTime", 1, pages));
		this.getMessageService().updateInboxMessageIsnew(userSession.getUid(), 0);
		return "inbox";
	}
	
	public String outbox(){
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.USER_PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/user/message.action?action=outbox");
		this.setPageList(this.getMessageService().findMessages(userSession.getUid(), -1, "outbox", -1, 0, "addTime", 1, pages));
		return "outbox";
	}
	
	public String add(){
		this.getSession().put("code", "");
		return "add";
	}
	
	public String save(){
		//验证码正确
		if(!getSessionAuthCode().equalsIgnoreCase(verifycode)){
			this.addActionError(this.getText("error.reg.auth"));
			this.getSession().put("code", "auto_error");
	        return "add";
		}
		UserInfo toUser = this.getUserInfoService().findUserInfoByUsername(this.username);
		if(toUser != null){
			Message inbox = new Message();
			inbox.setFromuid(userSession.getUid());
			inbox.setTouid(toUser.getId());
			inbox.setFolder("inbox");
			inbox.setMessage(this.message);
			inbox.setIsnew(1);
			inbox.setDelstatus(0);
			inbox.setAddTime(new Date());
			
			Message outbox = new Message();
			outbox.setFromuid(userSession.getUid());
			outbox.setTouid(toUser.getId());
			outbox.setFolder("outbox");
			outbox.setMessage(this.message);
			outbox.setIsnew(0);
			outbox.setDelstatus(0);
			outbox.setAddTime(new Date());
			
			this.getMessageService().createMessage(inbox, outbox);
			this.getSession().put("code", "message_create");
			return "add";
		}else{
			this.getSession().put("code", "user_error");
			return "add";
		}	
	}
	
	public String del(){
		Message m = this.getMessageService().findMessageById(this.id);
		if(m.getFromuid() == userSession.getUid() && m.getFolder().equalsIgnoreCase("outbox")){
			m.setDelstatus(1);
			this.getMessageService().updateMessage(m);
		}
		if(m.getTouid() == userSession.getUid() && m.getFolder().equalsIgnoreCase("inbox")){
			m.setDelstatus(1);
			this.getMessageService().updateMessage(m);
		}
		this.message = "{success:'true'}";
		return "message";
	}

	private String getSessionAuthCode() {
        String auth = (String) this.getSession().get("authCode");
        if(StringUtils.isBlank(auth)){
        	return "";
        }else{
        	return auth;
        }
    }
}
